package org.xteam.camllight;

public class Value {
	
	public static final int SIZEOF_VALUE = 4;
	public static final int SIZEOF_VALUE64 = 8;
	public static final Atom ValFalse = Atom.get(0);
	public static final Atom ValTrue = Atom.get(1);

	public static boolean isLong(int x) {
		return (x & 1) == 1;
	}
	
	public static boolean isBlock(int x) {
		return (x & 1) == 0;
	}

	public static int Bsize_wsize(int whsize) {
		return whsize * SIZEOF_VALUE;
	}

	public static int Wosize_whsize(int whsize) {
		return whsize - 1;
	}

	public static Object valLong(int value) {
		return new Integer((value<<1) + 1);
	}
	
	public static int longVal(Integer value) {
		return value>>1;
	}

	public static Object field(Object env, int i) {
		if (env instanceof Block) {
			return ((Block)env).field(i);
		}
		throw new RuntimeException("object has no field");
	}
	
	public static int tagOf(Object object) {
		if (object instanceof Block) {
			return ((Block)object).tag();
		}
		if (object instanceof Atom) {
			return ((Atom)object).tag();
		}
		throw new RuntimeException("object has no tag : " + object);
	}

	public static int sizeOf(Object env) {
		if (env instanceof Block) {
			return ((Block)env).size();
		}
		return 0;
	}

	public static Object get(Object env, int i) {
		if (env instanceof Block) {
			return ((Block)env).field(i);
		}
		return Atom.get(0);
	}

	public static void setField(Object object, int i, Object value) {
		if (object instanceof Block) {
			((Block)object).setField(i, value);
		}
	}

}
